import HttpUtils from '../HttpUtils/HttpUtils';
// 获取当前时间戳
var timestamp = Date.parse(new Date()) / 1000;
Page({
	...HttpUtils.func,
	data: {
		delete: "deleteHide",
		cancel: "删除",
		msgList: [],
		totalPages: 1,
		class: "",
		pageIndex: 1,
		pageItems: 10,
		toolbarList: [
			{
				btnName: "全部",
				clsName: "titOn"
			},
			{
				btnName: "通知",
				clsName: ""
			},
			{
				btnName: "提醒",
				clsName: ""
			},
			{
				btnName: "系统",
				clsName: ""
			}
		],
	},
	handleTap1(e) {   //会议筛选
		var btnName = e.currentTarget.dataset.name;
		var toolbarList = this.data.toolbarList;
		for (var index in toolbarList) {
			var toolBar = toolbarList[index];
			if (btnName == toolBar.btnName) {
				toolBar.clsName = "titOn";
			} else {
				toolBar.clsName = "";
			}
		}
		var msgTypes;
		//点击消息更改消息类型
		if (btnName == "通知") {
			msgTypes = '5';
		} else if (btnName == "提醒") {
			msgTypes = '6'
		} else if (btnName == "系统") {
			msgTypes = '4'
		} else {
			msgTypes = ''
		}
		this.setData({
			toolbarList: toolbarList,
			pageIndex: 1,
			msgType: msgTypes
		});
		//加载列表
		this.loadMsg(e);

	},
	delete(e) {
		this.setData({
			cancel: "取消",
			delete: "delete"
		});
		var msgId = e.currentTarget.dataset.code;
		//调用接口,删除消息
		this.delMsg(msgId);
	},
	//删除消息
	delMsg: function(msgId) {
		var newUrl = getApp().globalData.dangjianUrl + "/member/delUserMsg";
		var params = {};
		params["msgId"] = msgId + "";
		params = JSON.stringify(params);
		this.httpGet(newUrl, params, "删除消息失败").then(
			(respData) => {
				var result = respData.data.result.code;
				if (1 == result) {
					//列表中的消息移除,避免再调用接口获取数据add by dingyf 20190505
					var msgList = this.data.msgList;
					var newMsgList = [];
					for (var index in msgList) {
						if (msgId != msgList[index].msgId) {
							newMsgList.push(msgList[index]);
						}
					}
					this.setData({
						"msgList": newMsgList,
					});
				} else {
          this.showPageMsg("alert", respData.data.result.description)
				}
			}
		);
	},
	//取消
	cancel(e) {
		var index = e.target.dataset.index;
		var msgList = this.data.msgList;
		var msg = msgList[index];

		var deleteShow = msg.deleteShow;
		if (deleteShow == "deleteHide") {
			msg.deleteShow = "delete";
			msg.buttonName = "取消";
		} else {
			msg.deleteShow = "deleteHide";
			msg.buttonName = "删除";
		}
		msgList[index] = msg;
		this.setData({
			'msgList': msgList,
		});
	},
	//消息详情
	messDetail(e) {
    var msgType = e.currentTarget.dataset.msgtype;
		var msgId = e.currentTarget.dataset.msgid;
    var msgTitle = e.currentTarget.dataset.msgtitle;
		var msgContent = e.currentTarget.dataset.msgcontent;
		var publishTime = e.currentTarget.dataset.publishtime
    var creator = e.currentTarget.dataset.creator
		var otherId = e.currentTarget.dataset.otherid
		wx.navigateTo({
			url: '../messDetail/messDetail?msgId=' + msgId +
				'&creator=' + creator +
				'&msgTitle=' + msgTitle +
				'&msgContent=' + msgContent +
				'&msgType=' + msgType +
				'&otherId=' + otherId +
				'&publishTime=' + publishTime
		})
	},
	onLoad() {
		var arr = this.data.toolbarList;
		for (var i in arr) {
			if (arr[i].clsName == "titOn") {
				var btnName = arr[i].btnName
				var msgTypes;
				if (btnName == "通知") {
					msgTypes = '5';
				} else if (btnName == "提醒") {
					msgTypes = '6'
				} else if (btnName == "系统") {
					msgTypes = '4'
				} else {
					msgTypes = ''
				}
				this.setData({
					msgType: msgTypes
				});
			}
		};

		this.loadMsg();
	},
  onReachBottom() {
		// 页面被拉到底部
		if (this.data.pageIndex > this.data.totalPages) {
      this.showPageMsg("tips", "没有更多数据了!");
		} else {
			this.loadMsg();
		}
	},
	onPullDownRefresh: function() {
		this.setData({
			pageIndex: 1,
			msgList: []
		});
		this.loadMsg();
		wx.stopPullDownRefresh();
	},
	loadMsg: function(e) {
		if (e != undefined) {
			this.setData({
				msgList: []
			})
		}
		var newUrl = getApp().globalData.dangjianUrl + "/member/userMsgList";
		var params = {};
		params["pageIndex"] = this.data.pageIndex + "";
		params["pageItems"] = this.data.pageItems + "";
		params["msgType"] = this.data.msgType + "";
		params["msgTitle"] = "";
		params["read"] = "";
		params = JSON.stringify(params);
		this.httpGet(newUrl, params, "消息列表加载失败").then(
			(respData) => {
				var result = respData.data.result.code;
				if (1 == result) {
					var msgs = respData.data.msgs.msg;
					var pageIndex = respData.data.msgs.pageIndex + 1;
					var totalPages = respData.data.msgs.totalPages;
					var msgList = [];
					for (var index in msgs) {
						var msg = msgs[index];
						var json = {};
						json["otherId"] = msg.otherId;
						json["creatorId"] = msg.creatorId;
						json["creator"] = msg.creator;
						json["msgId"] = msg.msgId;
						json["msgTitle"] = msg.msgTitle;
						json["msgContent"] = msg.msgContent;
						json["publishTime"] = this.formatDate("yyyy-MM-dd hh:mm", msg.publishTime);
						json["isRead"] = msg.isRead;
						json["class"] = msg.isRead == 1 ? "noRead" : "";
						var type = msg.msgType;
						json["msgType"] = msg.msgType;
						if (type == 1) {
							json["msgTypeMame"] = "学习任务";
						} else if (type == 2) {
							json["msgTypeMame"] = "支部会议";
						} else if (type == 3) {
							json["msgTypeMame"] = "学习考试";
						} else if (type == 4) {
							json["msgTypeMame"] = "党员信息";
						} else if (type == 5) {
							json["msgTypeMame"] = "通知公告";
						} else if (type == 6) {
							json["msgTypeMame"] = "支部新闻";
						} else if (type == 7) {
							json["msgTypeMame"] = "投票通知";
						}
						//按钮
						json["buttonName"] = "删除";
						json["deleteShow"] = "deleteHide";
						msgList.push(json);
					}
					var oldMsgList = this.data.msgList;
					msgList = oldMsgList.concat(msgList);
					this.setData({
						"msgList": msgList,
						"pageIndex": pageIndex,
						"totalPages": totalPages
					});
				} else {
          this.showPageMsg("alert", respData.data.result.description)
				}
			}
		);
	}
});
